/************************************************************************
 * 					SRLOS TEAM ALL RIGHT RESERVED
 *@name:	srlfs.c
 *@author:	bloceanc
 *@date:	08/25/2010
 *@comment:	All string used in this file as parameters, must be UNICODE string
 ************************************************************************/

#include "srlfs_def.h"
#include "srlfs.h"

extern void *kernel_allocate(int size);
extern void *kernel_free(void *address);
extern int nandflash_read_page(void *address, unsigned char *readbuf, int readlength);
extern int nandflash_read_oob(void *address, unsigned char *readbuf, int readlength);
extern int nandflash_checkinvalid(void *page_addr);

static root_info	*root;

/* load root information */
void srlfs_load(void)
{
	unsigned int root_start_addr	= _SRLFS_START_ADDRESS_;
	unsigned int root_end_addr		= _SRLFS_END_ADDRESS_;

	root = (root_info *)kernel_allocate(sizeof(root_info));
	if(root == NULL)
	{
		while(1);
	}
	
	while(root_start_addr != _SRLFS_END_ADDRESS_)
	{
		if(nandflash_checkinvalid(root_start_addr))
		{
			continue;
		}
		if(nandflash_read_page(root_start_addr, (unsigned char *)root, sizeof(root_info)) != sizeof(root_info))
		{
			while(1);
		}
		break;
	}
	
	// scan nandflash, and recorgnize every partition information. */
}

/* 
 * find file or directory 
 * 		in path, '*' and '?' are not allowed
 * this routine just used to open an exist file
 * 
 */
FHANDLE srlfs_openfile(unsigned short *path, unsigned char openflag)
{
	unsigned int i;
	partition_info *partition;
	file_descriptor * file = kernel_allocate(sizeof(file_descriptor));
	if(file == NULL)
		return FS_NOMOREMEMORY;
	
	// find file
	for(i = 0; i < _SRLFS_ROOT_DRIVER_MAX_COUNT_; i++)
	{
		if(root->part_info[i].valid_flag && root->part_info[i].part_id == path[0])
		{
			partition = &root->part_info[i];
			break;
		}
	}
	
	if(i == _SRLFS_ROOT_DRIVER_MAX_COUNT_)
	{
		return FS_INVALIDPATH;
	}
	
	
}
